<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Auth\RegisterController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Validator;

class OrdersController extends Controller
{
    //输卡劵，密码
    public function card_quo(Request $request){
        $validator = Validator::make($request->all(), [
            'account' => 'required',
            'password' => 'required',
        ]);
        if ($validator->fails()) {
            return "<script>alert('卡劵号和密码不能为空');location.href='orderusepage';</script>";
        }
        $code = $this->validate($request, [
            'captcha' => 'required|captcha'
        ]);
        $account=$request['account'];
        $password=$request['password'];
        $data=DB::table('cards')->where('account',$account)->where('password',$password)->first();
        if($data->status==2){
            return "<script>alert('卡劵已被使用');location.href='orderusepage';</script>";
        }
        //$datas = DB::table('china_area')->where('parent_id', $request['id'])->get();
        //获取省
       /* print_r($province);
        return ;*/
        if ($data==null){
            return "<script>alert('账号或着卡密出错');location.href='orderusepage';</script>";
        }else{

            if (!$res=Schema::hasTable('china_area')){
                DB::unprepared(file_get_contents('china_area.sql'));

            }
            $province = DB::table('china_area')->where('id','>','1')->where('id','<','36')->get();
            return view('orderuse',['data'=>$data,/*'cityData'=>$datas,*/'province'=>$province]);
        }
    }
    //地址等得详细信息处理
    public function message_process(Request $request){
        $id=$request['id'];
        $arr['c_account']=$request['account'];
        $arr['name']=$request['name'];
        $arr['tel']=$request['tel'];
        $arr['province']=$request['province'];
        $arr['city']=$request['city'];
        $arr['area']=$request['area'];
        $arr['address']=$request['address'];
        $arr['order_mark']=substr($arr['tel'],-3).substr($arr['c_account'],-3).rand(0000,9999);
        $arr['created_at']=time();
        $arr['updated_at']=time();
        $res = DB::table('orders')->insert($arr);
        if($res){
            DB::table('cards')->where('id',$id)->update(['status'=>2]);
            $data=DB::table('cards')->where('account',$arr['c_account'])->first();
            $p_id=$data->p_id;
            $data=DB::table('products')->where('id',$p_id)->first();
            $inventory=$data->inventory;
            DB::table('products')->where('id',$p_id)->update(['inventory'=>$inventory-1]);
            $order_data=DB::table('orders')->where('c_account',$arr['c_account'])->first();
            $china_data=DB::table('china_area')->get();
            $province_id=$order_data->province;
            print_r($province_id);
            $city_id=$order_data->city;
            $area_id=$order_data->area;
            $datas=new \stdClass();
            $datas->province=$china_data[$province_id-1]->name;
            $datas->city=$china_data[$city_id-1]->name;
            if($area_id-1>0){
                $datas->area=$china_data[$area_id-1]->name;
            }
            $datas->area="";
            return view('order_message',['data'=>$order_data,'datas'=>$datas]);
        }

    }

    //省市区三级联动
    public function getCity(Request $request)
    {
        $province = $request['province'];

        $citys = DB::table('china_area')->where('parent_id','=',$province)->get()->toArray();

        return json_encode(['status' => 0, 'data' => $citys]);
    }

    public function getArea(Request $request)
    {
        $city = $request['city'];

        $area = DB::table('china_area')->where('parent_id','=',$city)->get()->toArray();

        return json_encode(['status' => 0, 'data' => $area]);
    }
}

